package com.promise.jokerdream.work.penguin

import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.repository.PhoneGameApi
import com.promise.jokerdream.database.entity.CurrencyExchangeGameConfigEntity
import com.promise.jokerdream.model.currency.CurrencyExchangeAmount
import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.foldWithApiException
import com.promise.jokerdream.utils.AppContext

/**
 * 货币兑换工作
 * 根据配置和活跃度查询结果来决定是否进行货币兑换
 */
class CurrencyExchangeWork(
    private val config: CurrencyExchangeGameConfigEntity,
    override val uin: String,
): BaseWork(uin) {
    
    override val name: String = "每日鹅币"
    override val priority: Int = 3 // 设置较高优先级
    
    private val gameApi = GameApi.getInstance()
    private val phoneGameApi = PhoneGameApi.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }
    
    override suspend fun executeWork(): WorkResult {
        // 1. 查询活跃度信息
        val queryResult = gameApi.queryLiveness()
        return queryResult.foldWithApiException(
            onSuccess = { response ->
                // 2. 根据配置和活跃度状态决定是否兑换
                val currencyExchangeAmount = try { CurrencyExchangeAmount.valueOf(config.currencyExchangeAmount) } catch (e: Exception) { CurrencyExchangeAmount.HUNDRED }
                when (currencyExchangeAmount) {
                    CurrencyExchangeAmount.HUNDRED -> {
                        // 如果配置了100斗豆且未兑换过
                        if (response.doudou100s == 0) {
                            executeCurrencyExchange(100) // 1鹅币兑换100斗豆
                        } else {
                            WorkResult.Success
                        }
                    }
                    CurrencyExchangeAmount.FIVE_HUNDRED -> {
                        // 如果配置了500斗豆且未兑换过
                        if (response.doudou500s == 0) {
                            executeCurrencyExchange(500) // 5鹅币兑换500斗豆
                        } else {
                            WorkResult.Success
                        }
                    }
                }
            },
            onFailure = { code, msg ->
                historyManager.addHistory(name, msg, code)
                WorkResult.Failure(Exception(msg), msg)
            }
        )
    }
    
    /**
     * 执行货币兑换
     * @param paynum 鹅币数量
     * @return 兑换结果
     */
    private suspend fun executeCurrencyExchange(paynum: Int): WorkResult {
        val exchangeResult = phoneGameApi.currencyExchange(uin, paynum)
        return exchangeResult.foldWithApiException(
            onSuccess = { response ->
                historyManager.addHistory(name, response.actionResult)
                WorkResult.Success
            },
            onFailure = { code, msg ->
                historyManager.addHistory(name, msg, code)
                WorkResult.Failure(Exception(msg), msg)
            }
        )
    }
}